package com.tl.sun.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tl.sun.AppConfig;
import com.tl.sun.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by apple on 16/10/11.
 */

public class HelperFragment extends Fragment {
    @Bind(R.id.wv_helper)
    WebView mHelperWebView;
    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.activity_helpe, container, false);
            ButterKnife.bind(this, rootView);

            init();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    public void init() {
        mRlGenericBack.setVisibility(View.GONE);
        mRlGenericBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mHelperWebView != null && mHelperWebView.canGoBack()) {
                    mHelperWebView.goBack();
                } else {
                    mRlGenericBack.setVisibility(View.GONE);
                }
            }
        });
        mTvGenericTitle.setText("帮助中心");
        WebSettings webSettings = mHelperWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        mHelperWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (mHelperWebView.canGoBack()) {
                    mRlGenericBack.setVisibility(View.VISIBLE);
                   // getFocus(true);
                } else {
                    mRlGenericBack.setVisibility(View.GONE);
                   // getFocus(false);
                }

            }
        });
        mHelperWebView.loadUrl(AppConfig.getInstance().getAppInfo().getHelpUrl() + "?origin=android");

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mHelperWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0) {
                    if (mHelperWebView != null && mHelperWebView.canGoBack()) {
                        mHelperWebView.goBack();
                    } else {

                    }
                    return true;
                }
                return  false;
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }



    //主界面获取焦点
    private void getFocus(final boolean b) {
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                    // 监听到返回按钮点击事件
                    mHelperWebView.goBack();
                    return true;
                }
                return b;
            }
        });
    }
}
